from stanfordcorenlp import StanfordCoreNLP
import logging
from urllib.parse import quote
import pickle
import re
import json

#nlp = StanfordCoreNLP('/data/liuhongfei/corenlp/4.2.2', logging_level=logging.DEBUG)
nlp = StanfordCoreNLP('http://localhost', 9000, logging_level=logging.DEBUG)


def corenlp_process(path):
    """process sample with StanforCoreNLP, input: str"""
    with open(path, 'r') as f:
        sample = f.read()
    props = {
        'annotators': 'tokenize,ssplit,pos,lemma,ner,parse,coref,dcoref',
        'outputFormat': 'json',
        'inputFormat': 'text',
    }
    output = nlp.annotate(quote(sample), properties=props)
    return output


if __name__ == '__main__':
    result = corenlp_process('/data/liuhongfei/cnn/stories/2258cb5afc7b74585f5b621e90abf367b25d7838.story')
    with open('test.json', 'w') as f:
        f.write(result)
    print('done')
